package com.woniuxy;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;



@FunctionalInterface
interface MyInterface<T> {
    boolean test(T t);

    default boolean test2(T t){
        return true;
    }

}
public class Test01 {

    public static void main(String[] args) {
        test02();
    }

    private static void test02() {
        List<Author> authors = getAuthors();
//        System.out.println(filter01(authors));
//        System.out.println(filter02(authors));
//
//        MyInterface<Author> myInterface = new MyInterface<Author>() {
//            @Override
//            public boolean test(Author author) {
//                return author.getAge() > 18;
//            }
//        };
//
//        System.out.println(filter(authors, myInterface));
//        System.out.println(filter(authors, new MyInterface<Author>() {
//            @Override
//            public boolean test(Author author) {
//                return author.getAname().startsWith("w");
//            }
//        }));

        System.out.println(filter(authors, author -> author.getAge() > 18));
        System.out.println(filter(authors,
                (Author author ) -> {return author.getAname().startsWith("w");}));
        System.out.println(filter(authors, author  -> author.getAname().startsWith("w")));
    }

    static List<Author> filter(List<Author> authos,MyInterface<Author> myInterface) {
        List<Author> findAuthors = new ArrayList<>();
        for(Author author : authos){
            if(myInterface.test(author)){
                findAuthors.add(author);
            }
        }
        return findAuthors;
    }

    //查询姓王的人
    static List<Author> filter02(List<Author> authos){
        List<Author> findAuthors = new ArrayList<>();
        for(Author autho : authos){
            if(autho.getAname().startsWith("w")){
                findAuthors.add(autho);
            }
        }
        return findAuthors;
    }

    //年龄>18岁的作者
    static List<Author> filter01(List<Author> authos){
        List<Author> findAuthors = new ArrayList<>();
        for(Author autho : authos){
            if(autho.getAge() > 18){
                findAuthors.add(autho);
            }
        }
        return findAuthors;
    }

    private static void test01() {
        List<String> strs=new ArrayList<>();
        strs.add("aaa");
        strs.add("bbb");
        strs.add("ccc");
        strs.add("ddd");

        Collections.sort(strs, (o1, o2) -> o1.compareTo(o2)*-1);
        System.out.println(strs);
    }

    private static List<Author> getAuthors() {
        List<Book> books1 = new ArrayList<>();
        List<Book> books2 = new ArrayList<>();
        List<Book> books3 = new ArrayList<>();
        List<Book> books4 = new ArrayList<>();
        List<Book> books5 = new ArrayList<>();

        books1.add(new Book(1, "书名1", 45));
        books1.add(new Book(2, "书名2", 66));

        books2.add(new Book(3, "书名3", 25));
        books2.add(new Book(4, "书名4", 78));

        books3.add(new Book(5, "书名5", 56));
        books3.add(new Book(6, "书名6", 45));

        books4.add(new Book(7, "书名7", 90));
        books4.add(new Book(8, "书名8", 123));

        books5.add(new Book(9, "书名9", 145));
        books5.add(new Book(10, "书名10", 35));

        Author author1 = new Author(1, "zs", 15, books1);
        Author author2 = new Author(2, "ls", 18, books2);
        Author author3 = new Author(3, "ww", 20, books3);
        Author author4 = new Author(4, "zl", 21, books4);
        Author author5 = new Author(5, "hq", 16, books5);

        return new ArrayList<>(Arrays.asList(author1, author2, author3, author4, author5));
    }
}


